#!/bin/bash

if ! [ -n "$1" ];
then
  echo "global.properties file not found"
  exit 1
fi

exportParams(){
  PARAM_FILE=$1
  ! [ -a ${PARAM_FILE} ] && return 1
  PARAMS=($(cat ${PARAM_FILE} | grep -v '#' | awk NF))
  for PARAM in "${PARAMS[@]}"
  do
    KEY=$(echo "${PARAM}" | awk -F '=' '{print $1}')
    VALUE=$(echo "${PARAM}" | awk -F '=' '{print $2}')
    eval "export ${KEY}=${VALUE}"
  done
}

#读取全局参数配置文件
exportParams $1

#分割应用
OLD_IFS="$IFS"
IFS=","
APPLICATIONS=(${APPLICATIONS})
IFS="$OLD_IFS"


main(){
  #检查执行文件是否存在
  export CHECK_SHELL=${APP_SCRIPT_DIR}/${APP_NAME}_check.sh
  ! [ -a ${CHECK_SHELL} ] && echo "${APP_INFO} not found check shell" && return 1
  chmod 777 ${CHECK_SHELL}

  export UNINSTALL_SHELL=${APP_SCRIPT_DIR}/${APP_NAME}_uninstall.sh
  [ -a ${UNINSTALL_SHELL} ] && chmod 777 ${UNINSTALL_SHELL}

  export INSTALL_SHELL=${APP_VERSION_DIR}/${APP_NAME}_${APP_VERSION}_install.sh
  ! [ -a ${INSTALL_SHELL} ] && INSTALL_SHELL=${APP_SCRIPT_DIR}/${APP_NAME}_install.sh
  ! [ -a ${INSTALL_SHELL} ] && echo "${APP_INFO} not found install shell" && return 1
  chmod 777 ${INSTALL_SHELL}

  export STOP_SHELL=${APP_VERSION_DIR}/${APP_NAME}_${APP_VERSION}_stop.sh
  ! [ -a ${STOP_SHELL} ] && STOP_SHELL=${APP_SCRIPT_DIR}/${APP_NAME}_stop.sh
  [ -a ${STOP_SHELL} ] && chmod 777 ${STOP_SHELL}

  export START_SHELL=${APP_VERSION_DIR}/${APP_NAME}_${APP_VERSION}_start.sh
  ! [ -a ${START_SHELL} ] && START_SHELL=${APP_SCRIPT_DIR}/${APP_NAME}_start.sh
  [ -a ${START_SHELL} ] && chmod 777 ${START_SHELL}

  export CONFIG_SHELL=${APP_VERSION_DIR}/${APP_NAME}_${APP_VERSION}_config.sh
  ! [ -a ${CONFIG_SHELL} ] && CONFIG_SHELL=${APP_SCRIPT_DIR}/${APP_NAME}_config.sh
  [ -a ${CONFIG_SHELL} ] && chmod 777 ${CONFIG_SHELL}

  echo "check installed"
  ${CHECK_SHELL}
  if ! [ $? -eq 0 ];
  then
    if [ ${UNINSTALL_IF_INSTALLED} == YES ];
    then
        ! [ -a ${UNINSTALL_SHELL} ] && echo "${APP_INFO} need uninstall but not found uninstall shell" && return 1
        chmod 777 ${UNINSTALL_SHELL}
        echo "start uninstall"
        ${UNINSTALL_SHELL}
    else
        return 0
    fi
  fi
  #刷新yum 缓存
  yum clean all >> ${YUM_LOG_FILE}
  chmod 777 ${INSTALL_SHELL}
  echo "start install"
  ${INSTALL_SHELL}

  echo "check install success"
  ${CHECK_SHELL}
  if [ $? -eq 0 ];
  then
      echo "install ${APP_NAME}:${APP_VERSION} failed"
      return 1
  fi
  echo "install success"

  [ -a ${STOP_SHELL} ] && chmod 777 ${STOP_SHELL} && echo "stop application" && ${STOP_SHELL}
  [ -a ${CONFIG_SHELL} ] && chmod 777 ${CONFIG_SHELL} && echo "config application" && ${CONFIG_SHELL}
  [ -a ${START_SHELL} ] && chmod 777 ${START_SHELL} && echo "start application" && ${START_SHELL}

  return 0
}


for APP in "${APPLICATIONS[@]}"
do
  OLD_IFS="$IFS";IFS=":";APP_INFOS=(${APP});IFS="$OLD_IFS"
  export APP_NAME=$(echo "${APP}" | awk -F ':' '{print $1}')
  [ ${#APP_INFOS[@]} -eq 2 ] && APP_VERSION=$(echo "${APP}" | awk -F ':' '{print $2}') || APP_VERSION=$(cat "${ROOT_DIR}/apps/${APP_NAME}/default_version")
  export APP_VERSION
  export APP_INFO=${APP_NAME}:${APP_VERSION}
  #所安装的应用所在的文件夹
  export APP_DIR="${ROOT_DIR}/apps/${APP_NAME}"
  #所安装的应用脚本所在的文件夹
  export APP_SCRIPT_DIR="${ROOT_DIR}/apps/${APP_NAME}/script"
  #所安转的应用的版本所在的文件夹
  export APP_VERSION_DIR="${APP_DIR}/${APP_VERSION}"
  #安装输出信息保存的文件夹位置（日志+必要信息+安装信息等等
  export APP_OUTPUT_DIR=${OUTPUT_DIR}/${APP_NAME}
  #重新创建文件夹
  rm -rf ${APP_OUTPUT_DIR}
  mkdir -p ${APP_OUTPUT_DIR}
  #依赖文件夹位置
  export DEPENDENCE_DIR=${APP_VERSION_DIR}/dependence

  #将APP_VERSION内的ini配置信息刷一遍
  exportParams "${APP_VERSION_DIR}/${APP_NAME}_${APP_VERSION}_config.ini"

  echo "---------------APP_INFO:${APP_INFO}---------------"
  export YUM_LOG_FILE=${APP_OUTPUT_DIR}/yum_install.log
  export INFO_FILE=${APP_OUTPUT_DIR}/${APP_NAME}.info
  MAIN_LOG_FILE=${APP_OUTPUT_DIR}/${APP_NAME}_main.log

  eval "main $(! [ ${PRINT_LOG_ON_CONSOLE} == YES ] &&  echo "> ${MAIN_LOG_FILE}" )"

done